﻿using System;
using System.Xml.Linq;
using LINQPad.Extensibility.DataContext;

namespace NoRMLinqpadDriver
{
    public class NoRMProperties
    {
        private readonly IConnectionInfo cxInfo;
        readonly XElement driverData;
        
        /// <summary>
        /// Initializes a new instance of the <see cref="NoRMProperties"/> class.
        /// </summary>
        /// <param name="cxInfo">The cx info.</param>
        public NoRMProperties(IConnectionInfo cxInfo)
        {
            this.cxInfo = cxInfo;
            driverData = cxInfo.DriverData;
        }

        /// <summary> Gets or sets a value indicating whether this <see cref="NoRMProperties"/> is persist. </summary>
        public bool Persist
        {
            get { return CxInfo.Persist; }
            set { CxInfo.Persist = value; }
        }

        /// <summary>ConnectionString</summary>
        public string ConnectionString
        {
            get { return (string)driverData.Element("ConnectionString") ?? string.Empty; }
            set { driverData.SetElementValue("ConnectionString", value); }
        }

        /// <summary>Path to the assembly containing the actual entities</summary>
        public string EntityAssemblyPath
        {
            get { return (string)driverData.Element("EntityAssemblyPath") ?? string.Empty; }
            set { driverData.SetElementValue("EntityAssemblyPath", value); }
        }

        /// <summary>Semicolon separated list of Namespaces within the EntityAssembly that contain the Entity Classes</summary>
        public string EntityAssemblyNamespaces
        {
            get { return (string)driverData.Element("EntityAssemblyNamespaces") ?? string.Empty; }
            set { driverData.SetElementValue("EntityAssemblyNamespaces", value); }
        }

        /// <summary>
        /// Gets the cx info.
        /// </summary>
        /// <value>The cx info.</value>
        public IConnectionInfo CxInfo
        {
            get { return cxInfo; }
        }
    }
}
